package com.corpgovernment.organization.check;

import java.util.Calendar;
import java.util.Date;

public class DateRangeOverlapChecker {

    private static final int DAYS_IN_YEAR = 365; // 假设一年有365天

    public static boolean hasOverlap(DateRange range1, DateRange range2) {
        boolean[] bitmap = new boolean[DAYS_IN_YEAR];
        markBitmap(bitmap, range1); // 标记第一个日期范围的位图
        return checkBitmap(bitmap, range2); // 检查第二个日期范围是否与位图重叠
    }

    private static void markBitmap(boolean[] bitmap, DateRange range) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(range.getStartDate());
        int startDayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(range.getEndDate());
        int endDayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        if (endDayOfYear < startDayOfYear) { // 处理跨年的情况
            for (int i = startDayOfYear; i <= DAYS_IN_YEAR; i++) {
                bitmap[i - 1] = true;
            }
            for (int i = 1; i <= endDayOfYear; i++) {
                bitmap[i - 1] = true;
            }
        } else {
            for (int i = startDayOfYear; i <= endDayOfYear; i++) {
                bitmap[i - 1] = true;
            }
        }
    }

    private static boolean checkBitmap(boolean[] bitmap, DateRange range) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(range.getStartDate());
        int startDayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(range.getEndDate());
        int endDayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        if (endDayOfYear < startDayOfYear) { // 处理跨年的情况
            for (int i = startDayOfYear; i <= DAYS_IN_YEAR; i++) {
                if (bitmap[i - 1]) {
                    return true;
                }
            }
            for (int i = 1; i <= endDayOfYear; i++) {
                if (bitmap[i - 1]) {
                    return true;
                }
            }
        } else {
            for (int i = startDayOfYear; i <= endDayOfYear; i++) {
                if (bitmap[i - 1]) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        DateRange range1 = new DateRange(makeDate( "11-05"), makeDate("05-05"));
        DateRange range2 = new DateRange(makeDate( "05-06"), makeDate("12-06"));

        Boolean res1 =  hasOverlap(range1, range2);

        DateRange range3 = new DateRange(makeDate("11-05"), makeDate( "11-07"));
        DateRange range4 = new DateRange(makeDate( "12-06"), makeDate("12-01"));

        Boolean res2 =  hasOverlap(range3, range4);

        System.out.println(1);
    }

    public static Date makeDate(String yy_dd) {
        String[] strings = yy_dd.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, Integer.parseInt(strings[0]) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strings[1]));
        return calendar.getTime();
    }

}

